package com.beiye.drivertransport.SubActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.NewHolehotelBean;
import com.beiye.drivertransport.utils.ChartUtilCallthepolice;
import com.githang.statusbar.StatusBarCompat;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;



//报警百分比
public class CallthepoliceActivity extends TwoBaseAty {

    LineChart line_frist1;//折线图

    BarChart bar_frist2;//柱状图

    TextView tv_des;//

    ImageView img_hiddenback;
    private XAxis xAxis;
    private YAxis leftAxis;
    private YAxis rightAxis;
    ArrayList<String> xVals = new ArrayList<String>();
    @Override
    public int getLayoutId() {
        return R.layout.activity_callthepolice;

    }

    @Override
    public void initData() {
        line_frist1 = (LineChart)findViewById(R.id.line_frist1);
        bar_frist2 = (BarChart)findViewById(R.id.bar_frist2);
        img_hiddenback = (ImageView) findViewById(R.id.img_hiddenback);
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        initLineChart();
        img_hiddenback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



        ChartUtilCallthepolice.initBarchart(bar_frist2);
    }

    @Override
    public void requestData() {
        Bundle bundle = getIntent().getExtras();
        List<NewHolehotelBean.DataBean.InfoLwStatNoBean> listCourse = (List<NewHolehotelBean.DataBean.InfoLwStatNoBean>) bundle.getSerializable("firmlist");
        String name = bundle.getString("name");
        if(listCourse.size()==0)
       {
         return;
       }
        if(name.equals("企业管理隐患统计"))
        {
            tv_des.setText("消除企业隐患报警数");
            ArrayList<Entry> entries = new ArrayList<Entry>();
            for (int i = 0; i < listCourse.size(); i++) {
                BarEntry barEntry = new BarEntry(i, (float) listCourse.get(i).getFirmStat2per());
                xVals.add( listCourse.get(i).getStatYmd());
                entries.add(barEntry);
            }
            // 每一个LineDataSet代表一条线
            LineDataSet lineDataSet = new LineDataSet(entries,"所辖区域企业隐患报警百分比");
            lineDataSet.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

                    int n = (int) value;
                    if(n==0)
                    {
                        return "0%";
                    }else{

                        return n+"%";
                    }
                }
            });
            //设置x轴的数据
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    //  return String.valueOf(xVals.get((int) value));
                    int index = (int) value;
                    if (index < 0 || index >= xVals.size()) {
                        return "";
                    } else {
                        return xVals.get(index);
                    }

                }
            });

            leftAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    return value + "%";
                }
            });
            initLineDataSet(lineDataSet, Color.parseColor("#6bbbec"), true);
            ArrayList<ILineDataSet> dataSets = new ArrayList<>();
            dataSets.add(lineDataSet);
            LineData data = new LineData(dataSets);

            //设置X轴的刻度数
           xAxis.setLabelCount(listCourse.size());
        //    line_frist1.moveViewToX(xVals.size() - 1);
            line_frist1.setData(data);
            line_frist1.invalidate();
            Collections.reverse(listCourse);
            ChartUtilCallthepolice.initCheckData(bar_frist2,listCourse,name);
        }else if(name.equals("从业人员管理隐患统计"))
        {
            tv_des.setText("消除从业人员隐患报警数");
            ArrayList<Entry> entries = new ArrayList<Entry>();
            for (int i = 0; i < listCourse.size(); i++) {
                BarEntry barEntry = new BarEntry(i, (float) listCourse.get(i).getDriverStat2per());
                xVals.add( listCourse.get(i).getStatYmd());
                entries.add(barEntry);
            }
            // 每一个LineDataSet代表一条线
            LineDataSet lineDataSet = new LineDataSet(entries,"所辖区域从业隐患报警百分比");
            lineDataSet.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

                    int n = (int) value;
                    if(n==0)
                    {
                        return "0%";
                    }else{

                        return n+"%";
                    }
                }
            });
            //设置x轴的数据
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {

                    int index = (int) value;
                    if (index < 0 || index >= xVals.size()) {
                        return "";
                    } else {
                        return xVals.get(index);
                    }
                }
            });


            leftAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    return value + "%";
                }
            });
            initLineDataSet(lineDataSet, Color.parseColor("#6bbbec"), true);
            ArrayList<ILineDataSet> dataSets = new ArrayList<>();
            dataSets.add(lineDataSet);
            LineData data = new LineData(dataSets);

            //设置X轴的刻度数
            xAxis.setLabelCount(listCourse.size(), true);
          //  line_frist1.moveViewToX(xVals.size() - 1);
            line_frist1.setData(data);
            line_frist1.invalidate();
            Collections.reverse(listCourse);
            ChartUtilCallthepolice.initCheckData(bar_frist2,listCourse,name);
        }
        else if(name.equals("车辆装备管理隐患统计"))
        {
            tv_des.setText("消除车辆隐患报警数");
            ArrayList<Entry> entries = new ArrayList<Entry>();
            for (int i = 0; i < listCourse.size(); i++) {
                BarEntry barEntry = new BarEntry(i, (float) listCourse.get(i).getVehStat2per());
                xVals.add( listCourse.get(i).getStatYmd());
                entries.add(barEntry);
            }
            // 每一个LineDataSet代表一条线
            LineDataSet lineDataSet = new LineDataSet(entries,"所辖区域车辆隐患报警百分比");
            lineDataSet.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

                    int n = (int) value;
                    if(n==0)
                    {
                        return "0%";
                    }else{

                        return n+"%";
                    }
                }
            });
            //设置x轴的数据
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {

                    int index = (int) value;
                    if (index < 0 || index >= xVals.size()) {
                        return "";
                    } else {
                        return xVals.get(index);
                    }
                }
            });


            leftAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    return value + "%";
                }
            });
            initLineDataSet(lineDataSet, Color.parseColor("#6bbbec"), true);
            ArrayList<ILineDataSet> dataSets = new ArrayList<>();
            dataSets.add(lineDataSet);
            LineData data = new LineData(dataSets);

            //设置X轴的刻度数
            xAxis.setLabelCount(listCourse.size(), true);
          //  line_frist1.moveViewToX(xVals.size() - 1);
            line_frist1.setData(data);
            line_frist1.invalidate();
            Collections.reverse(listCourse);
            ChartUtilCallthepolice.initCheckData(bar_frist2,listCourse,name);
        }

    }
    /**
     * 初始化曲线 每一个LineDataSet代表一条线
     *
     * @param lineDataSet
     * @param color
     * @param mode        折线图是否填充
     */
    private void initLineDataSet(LineDataSet lineDataSet, int color, boolean mode) {
        lineDataSet.setColor(color);
        lineDataSet.setCircleColor(color);
        lineDataSet.setLineWidth(1f);
        lineDataSet.setCircleRadius(3f);
        //设置曲线值的圆点是实心还是空心
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setValueTextSize(9f);
        //设置折线图填充
        lineDataSet.setDrawFilled(mode);
        lineDataSet.setFormLineWidth(1f);
        lineDataSet.setFormSize(15.f);
        //填充颜色.透明度
        // lineDataSet.setFillColor(Color.BLUE);
        //  lineDataSet.setFillAlpha(35);
        //线模式为圆滑曲线（默认折线）
        lineDataSet.setMode(LineDataSet.Mode.LINEAR);
    }
    /**
     * 初始化LineChart
     */
    private void initLineChart() {
        line_frist1.setDrawGridBackground(false);
        //显示边界
        line_frist1.setDrawBorders(false);
        line_frist1.getDescription().setEnabled(false);
        Description description = new Description();
        description.setText("");//设置右下角的描述信息
        line_frist1.setNoDataText("暂无数据"); //设置空表的描述信息
        line_frist1.setPinchZoom(true);//挤压缩放
        line_frist1.setDoubleTapToZoomEnabled(false);//双击缩放
        line_frist1.animateY(2500);
        //设置动画效果
//        mChart.animateY(1000, Easing.EasingOption.Linear);
//        mChart.animateX(1000, Easing.EasingOption.Linear);

        //折线图例 标签 设置
        Legend legend = line_frist1.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
//        mChart.setPinchZoom(true);//挤压缩放
//        mChart.setScaleYEnabled(false);
//        mChart.setDoubleTapToZoomEnabled(false);//双击缩放
        xAxis = line_frist1.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setTextSize(3f);

        leftAxis = line_frist1.getAxisLeft();
        rightAxis = line_frist1.getAxisRight();
        rightAxis.setDrawLabels(false);//右侧Y轴数据不显示
        rightAxis.setEnabled(false);
        //XY轴的设置
        //X轴设置显示位置在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        //保证Y轴从0开始，不然会上移一点
        leftAxis.setAxisMinimum(0f);
        //   rightAxis.setAxisMinimum(0f);
    }
}
